﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Globalization;
using System.Text.RegularExpressions;

namespace FinalProject.UI.Controls
{
    public class NumericTextBox : TextBox
    {
        public bool AllowNegative { get; set; }

        private NumberFormatInfo numberFormat;
        private string negativeSign, decimalSeperator, groupSeperator;

        public NumericTextBox()
            : base()
        {

        }

        protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
        {
            base.OnPreviewTextInput(e);
            string strInput = e.Text;
            string pattern = this.GetPatternString();
            if (!Regex.IsMatch(strInput, pattern))
                e.Handled = true;
            else
                e.Handled = false;
        }

        private string GetPatternString()
        {
            this.numberFormat = CultureInfo.CurrentCulture.NumberFormat;
            this.negativeSign = this.numberFormat.NegativeSign;
            this.decimalSeperator = this.numberFormat.CurrencyDecimalSeparator;
            this.groupSeperator = this.numberFormat.CurrencyGroupSeparator;
            StringBuilder pattern = new StringBuilder();
            pattern.Append("^");
            pattern.Append(@"\d");
            pattern.Append("$");

            return pattern.ToString();
        }
    }
}
